<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript">
			//location.href="https://www.pzhu.cn/"
		</script>
		
		<script type="text/javascript">
			function add1(){
				let text=document.createElement("p")
				text.innerHTML="文字节点"  //改变文本
				text.setAttribute("style","color: red;") //通过改变style属性来间接控制CSS样式
				document.getElementsByClassName("con")[0].appendChild(text)
			}
			function add2(){
				let img=document.createElement("img")
				img.setAttribute("src","../../src/ok.png") //改变/增加属性
				document.getElementsByClassName("con")[0].appendChild(img)
			}
			function del1(){
				let ftext=document.querySelector(".con>p")
				if(ftext){
					document.getElementsByClassName("con")[0].removeChild(ftext)
				}else{
					alert("没有可以删除的节点")
				}				
				//ftext.parentNode.removeChild(ftext)
			}
			function del2(){
				let fimg=document.querySelector(".con>img")
				document.getElementsByClassName("con")[0].removeChild(fimg)
				//fimg.parentNode.removeChild(fimg)
			}
			function del3(){
				let con=document.getElementsByClassName("con")[0]
				con.removeChild(con.firstChild)
			}
			function del3(){
				let con=document.getElementsByClassName("con")[0]
				con.removeChild(con.lastChild)
			}
		</script>
	</head>
	<body>
		<button type="button" onclick="add1()">添加文本</button>
		<button type="button" onclick="add2()">添加图片</button>
		<button type="button" onclick="del1()">删除文本</button>
		<button type="button" onclick="del2()">删除图片</button>
		<button type="button" onclick="del3()">删除第一个元素</button>
		<button type="button" onclick="del4()">删除最后一个元素</button>
		<div class="con"></div>
	</body>
</html>
